package userinfo.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import userinfo.Entity.UserInfo;
import userinfo.Mapper.UserInfoMapper;
import userinfo.Service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import userlogin.Entity.UserId;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小陈同学
 * @since 2023-06-26
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo getUserInfo(String openid) {
        //查询用户
        UserInfo userInfo = userInfoMapper.findUserById(openid);
        // 2.利用RestTemplate发起http请求，查询用户
        // 2.1.url路径
        String url = "http://userlogin/user-id/" + userInfo.getOpenid();
        System.out.println(url);
        // 2.2.发送http请求，实现远程调用
        UserId user = restTemplate.getForObject(url, UserId.class);
        // 3.封装user到userInfo
        userInfo.setUserId(user);
        // 4.返回
        return userInfo;
    }

    @Override
    public IPage<UserInfo> getPage(Integer currentPage, Integer PageSize, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(userInfo.getRealname()), UserInfo::getRealname, userInfo.getRealname());
        IPage page = new Page(currentPage, PageSize);
        return page;
    }

    @Override
    public IPage<UserInfo> getPage(Integer currentPage, Integer pageSize) {
        IPage page = new Page(currentPage, pageSize);
        userInfoMapper.selectPage(page, null);
        return page;
    }

}
